package com.sse.product.controller.rbac;


import com.sse.product.common.ResultCode;
import com.sse.product.rbac.entity.Role;
import com.sse.product.webservice.rbac.request.AddRoleFunctionRequest;
import com.sse.product.model.rbac.ResponseVo;
import com.sse.product.rbac.service.IRoleMenusService;
import com.sse.product.rbac.service.IRoleService;
import com.sse.product.webservice.rbac.request.RoleQueryRequest;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author junyongli
 * @since 2022-07-22
 */
@RestController
@RequestMapping("/rbac/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IRoleMenusService roleMenuService;

    @ApiOperation(value = "新增角色")
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('ROLE-MANAGE')")
    public ResponseVo addRole(@RequestBody Role role) {
        return ResponseVo.assemble(ResultCode.SUCCESS, roleService.addRole(role));
    }

    @ApiOperation(value = "更新角色")
    @PreAuthorize("hasAuthority('ROLE-MANAGE')")
    @PostMapping("/update")
    public ResponseVo updateRole(@RequestBody Role role) {
        return ResponseVo.assemble(ResultCode.SUCCESS, roleService.updateRole(role));
    }

    @ApiOperation(value = "删除角色")
    @PreAuthorize("hasAuthority('ROLE-MANAGE')")
    @PostMapping("/delete")
    public ResponseVo delete(@RequestBody Role role) {
        return ResponseVo.assemble(ResultCode.SUCCESS, roleService.delRol(role));
    }

    @ApiOperation(value = "根据角色名称模糊查询，无参数可返回全部角色")
    @PreAuthorize("hasAuthority('ROLE-MANAGE')")
    @PostMapping("/query")
    public ResponseVo queryByRoleName(@RequestBody RoleQueryRequest queryVO) {
        return ResponseVo.assemble(ResultCode.SUCCESS, roleService.queryRole(queryVO.getRoleNameLike()));
    }

    @ApiOperation(value = "根据角色(id)查询关联菜单")
    @PreAuthorize("hasAuthority('ROLE-MANAGE')")
    @PostMapping("/queryMenuByRole")
    public ResponseVo queryMenuByRole(@RequestBody Role role) {
        return ResponseVo.assemble(ResultCode.SUCCESS, roleMenuService.queryMenuByRoleId(role.getId()));
    }

    @ApiOperation(value = "绑定角色(id)和菜单(id)之间的关系【包括新增更新】")
    @PreAuthorize("hasAuthority('ROLE-MANAGE')")
    @PostMapping("/addRoleMenu")
    public ResponseVo addRoleMenu(@RequestBody AddRoleFunctionRequest request) {
        return ResponseVo.assemble(ResultCode.SUCCESS, roleMenuService.addRoleMenu(request.getRole(), request.getMenusList()));
    }

}
